package com.atcumt.Thread.print;

import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport 实现
 * 一个许可的信号量机制，park消费，unpark放入，每个线程仅一个，不累积
 */
public class testPrint02 {

    static int i = 0;
    static Thread t1, t2;

    public static void main(String[] args) {
        t1 = new Thread(() -> {
            while (i < 10) {
                System.out.println(Thread.currentThread().getName() + "---" + (++i));
                LockSupport.unpark(t2); // 唤醒 t2
                LockSupport.park(); // 阻塞 t1
            }
        });

        t2 = new Thread(() -> {
            while (i < 10) {
                LockSupport.park(); // 阻塞等待
                System.out.println(Thread.currentThread().getName() + "---" + (++i));
                LockSupport.unpark(t1); // 唤醒 t1
            }
        });
        t1.start();
        t2.start();
    }
}